package top.ijiujiu.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import top.ijiujiu.entity.SysBuriedLog;
import top.ijiujiu.exception.DefaultException;
import top.ijiujiu.service.ISysBuriedLogService;
import top.ijiujiu.util.ResultUtil;

import java.util.List;

/**
 * 系统埋点记录日志 前端控制器
 *
 * @Author 神秘的黄瓜
 * @Version 1.0
 * @Date 2020-04-10
 */
@Api(value = "系统埋点记录日志接口API", tags = {"系统埋点记录日志接口API"})
@RestController
@RequestMapping("sysBuriedLog")
public class SysBuriedLogController {

    private final static Logger LOGGER = LoggerFactory.getLogger(SysBuriedLogController.class);


    @Autowired
    private ISysBuriedLogService sysBuriedLogService;

    /**
     * 根据ID查询数据
     *
     * @param id
     * @return
     * @throws DefaultException
     */
    @ApiOperation(value = "根据ID查询数据", notes = "根据ID查询数据")
    @GetMapping("getOne/{id}")
    public ResultUtil<SysBuriedLog> getOne(@PathVariable("id") String id) throws DefaultException {
        if (StringUtils.isEmpty(id)){
            throw new DefaultException(HttpStatus.INTERNAL_SERVER_ERROR, "请求参数为空~~~~~~");
        }
        LOGGER.info("入参为:[{}]", id);
        ResultUtil<SysBuriedLog> data = new ResultUtil<>();
        return data.success(this.sysBuriedLogService.getById(id));
    }



    /**
     * 查询全部数据
     *
     * @return
     */
    @ApiOperation(value = "查询全部数据", notes = "查询全部数据")
    @GetMapping("getAll")
    public ResultUtil<List<SysBuriedLog>> getAll() {
        ResultUtil<List<SysBuriedLog>> data = new ResultUtil<>();
        return data.success(this.sysBuriedLogService.list());
    }


    /**
     * 新增一条数据
     *
     * @param param
     * @return
     * @throws DefaultException
     */
    @ApiOperation(value = "新增一条数据", notes = "新增一条数据")
    @PostMapping("add")
    public ResultUtil<Boolean> add(@RequestBody SysBuriedLog param) throws DefaultException {
        if (ObjectUtils.isEmpty(param)) {
            throw new DefaultException(HttpStatus.INTERNAL_SERVER_ERROR, "请求参数为空~~~~~~");
        }
        LOGGER.info("入参为:[{}]", param);
        ResultUtil<Boolean> data = new ResultUtil<>();
        return data.success(this.sysBuriedLogService.save(param));
    }


    /**
     * 修改一条数据
     *
     * @param param
     * @return
     * @throws DefaultException
     */
    @ApiOperation(value = "修改一条数据", notes = "修改一条数据")
    @PatchMapping("edit")
    public ResultUtil<Boolean> edit(@RequestBody SysBuriedLog param) throws DefaultException {
        if (ObjectUtils.isEmpty(param)) {
            throw new DefaultException(HttpStatus.INTERNAL_SERVER_ERROR, "请求参数为空~~~~~~");
        }
        LOGGER.info("入参为:[{}]", param);
        ResultUtil<Boolean> data = new ResultUtil<>();
        return data.success(this.sysBuriedLogService.updateById(param));
    }


    /**
     * 根据ID删除数据
     *
     * @param id
     * @return
     * @throws DefaultException
     */
    @ApiOperation(value = "根据ID删除数据", notes = "根据ID删除数据")
    @DeleteMapping("delete/{id}")
    public ResultUtil<Boolean> delete(@PathVariable("id") String id) throws DefaultException {
        if (StringUtils.isEmpty(id)){
            throw new DefaultException(HttpStatus.INTERNAL_SERVER_ERROR, "请求参数为空~~~~~~");
        }
        LOGGER.info("入参为:[{}]", id);
        ResultUtil<Boolean> data = new ResultUtil<>();
        return data.success(this.sysBuriedLogService.removeById(id));
    }


}

